/**
 * Wechat.java created by zhangzhidong 
 * at 上午11:07:18 2016年4月12日
 */
package pojo.weichatlab;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import cn.edu.bjtu.entity.IDocument;
/**
 * @author zhangzhidong
 *
 */
@Entity
@Table(name="wechat")
public class Wechat extends WechatBaseModel{
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY) 
	int id;
	String url;
	String title;
	String name;
	String account;
	String brief;
	String content;
	Date date;
	@OneToMany(cascade=CascadeType.ALL,targetEntity=WechatClassMap.class)
	@JoinColumn(name="wechatid")
	List<WechatClassMap> wechatclassmaps = new ArrayList<WechatClassMap>(); 
	public void addMap(WechatClassMap e){
		this.wechatclassmaps.add(e);
	}
	
	
	public Wechat() {
		// TODO Auto-generated constructor stub
	}
	public Wechat(int id,String url,String title,String name,String account,String brief,String content,Date date) {
		// TODO Auto-generated constructor stub
		this.id =  id;
		this.url = url;
		this.name = name;
		this.title = title;
		this.account = account;
		this.brief = brief;
		this.content = content;
		this.date = date;
	}
	public Wechat(String url,String title,String name,String account,String brief,String content,Date date) {
		// TODO Auto-generated constructor stub
		this.url = url;
		this.name = name;
		this.title = title;
		this.account = account;
		this.brief = brief;
		this.content = content;
		this.date = date;
	}
	/**
	 * @return the wechatclassmaps
	 */
	public List<WechatClassMap> getWechatclassmaps() {
		return wechatclassmaps;
	}
	/**
	 * @param wechatclassmaps the wechatclassmaps to set
	 */
	public void setWechatclassmaps(List<WechatClassMap> wechatclassmaps) {
		this.wechatclassmaps = wechatclassmaps;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}
	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}
	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}
	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the account
	 */
	public String getAccount() {
		return account;
	}
	/**
	 * @param account the account to set
	 */
	public void setAccount(String account) {
		this.account = account;
	}
	/**
	 * @return the brief
	 */
	public String getBrief() {
		return brief;
	}
	/**
	 * @param brief the brief to set
	 */
	public void setBrief(String brief) {
		this.brief = brief;
	}
	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}
	/**
	 * @param content the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}
	/**
	 * @return the date
	 */
	public Date getDate() {
		return date;
	}
	/**
	 * @param date the date to set
	 */
	public void setDate(Date date) {
		this.date = date;
	}
	public String toString(){
		return this.title+"****"+this.content+"\n"+this.date;
	}
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	
	public static IDocument toIdocument(Wechat w){
		return new IDocument() {
			@Override
			public String getTag() {
				return "unkonwn";
			}
			
			@Override
			public String getID() {
				return w.getUrl();
			}
			
			@Override
			public String getContent() {
				return w.getContent();
			}
		};
	}
	
	public static Wechat stringtoWechat(String line) {
		if(line == null) return null;
		String parts [] = line.split("\t");
		String url = new StringBuilder().append("http://mp.weixin.qq.com/s?__biz="+parts[0]).append("&mid="+parts[1]).append("&idx="+parts[2]).append("&sn="+parts[3]).toString();
		String title = parts[4];
		String name = parts[5];
		String account = parts[6];
		String brief = parts[7];
		String content = parts[8];
		Date date = new Date();
		try {
			date = new SimpleDateFormat("yyyyMMdd").parse(parts[9]);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Wechat(url, title, name, account, brief, content, date);
	}
	
	public String fulltoString(){
		return this.url+"\t"+this.title+"\t"+this.name +"\t"+this.account + "\t"+this.brief+"\t"+this.content+"\t"+new SimpleDateFormat("yyyyMMdd").format(this.date);
	}
	
}
